package com.example.service;

import com.example.entity.Friends;
import com.example.mapper.FriendsMapper;
import com.example.utils.BisException;
import com.example.utils.ResultData;
import com.example.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * The type Friends service.
 */
@Service
public class FriendsService {
    @Autowired
    private FriendsMapper friendsMapper;

    /**
     * Add friends result data.
     *
     * @param friends the friends
     * @return the result data
     */
    public ResultData addFriends(Friends friends) {
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        friends.setUserDate(sdf3.format(new Date()));
        int result = friendsMapper.insert(friends);
        if (result > 0) {
            return ResultUtils.sucuss("添加成功！！", result);
        } else {
            throw new BisException("添加失败");
        }
    }

    /**
     * Gets friends.
     *
     * @return the friends
     */
    public ResultData getFriends() {

        List<Friends> list = friendsMapper.selectList(null);
        if (!list.isEmpty()) {
            return ResultUtils.sucuss("查询成功！！", list);
        } else {
            throw new BisException("查询失败");
        }
    }

    /**
     * Del friends result data.
     *
     * @param friends the friends
     * @return the result data
     */
    public ResultData delFriends(Friends friends) {

        int result = friendsMapper.deleteById(friends.getId());
        if (result > 0) {
            return ResultUtils.sucuss("删除成功！！", result);
        } else {
            throw new BisException("删除失败");
        }
    }

    /**
     * Up friends result data.
     *
     * @param friends the friends
     * @return the result data
     */
    public ResultData UpFriends(Friends friends) {

        int result = friendsMapper.updateById(friends);
        if (result > 0) {
            return ResultUtils.sucuss("更新成功！！", result);
        } else {
            throw new BisException("更新失败");
        }
    }
}
